package es.tid.jala.android.conversor;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Conversor extends Activity {
	private EditText campoCel;
	private EditText campoFar;

    private static final int CEL_TO_FAR = Menu.FIRST;
    private static final int FAR_TO_CEL = Menu.FIRST + 1;
    private static final int SALIR = Menu.FIRST + 2;
	
    private static final int DIALOG_ALERTA_ERROR = 0;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
		campoCel = (EditText) findViewById(R.id.celsius);
		campoFar = (EditText) findViewById(R.id.farenheit);

		Button cel2far = (Button) findViewById(R.id.cel2far);
		Button far2cel = (Button) findViewById(R.id.far2cel);
		Button salir = (Button) findViewById(R.id.salir);

		cel2far.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				celsiusToFarenheit();
			}
		});

		far2cel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				farenheitToCelsius();
			}
		});

		salir.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				salir();
			}
		});		
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(Menu.NONE, CEL_TO_FAR, 0, R.string.CtoF);
        menu.add(Menu.NONE, FAR_TO_CEL, 1, R.string.FtoC);
        menu.add(Menu.NONE, SALIR, 2, R.string.salir);
        return true;    	
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	super.onMenuItemSelected(featureId, item);
    	switch(item.getItemId()) {
    	case CEL_TO_FAR:
    		celsiusToFarenheit();
    		break;
    	case FAR_TO_CEL:
    		farenheitToCelsius();
    		break;
    	case SALIR:
    		salir();
    		break;
    	}
    	
    	return true;
    }
    
	protected void celsiusToFarenheit() {
		String s = campoCel.getText().toString();
		if (s.equals("")) {
			campoFar.setText("");
			showDialog(Conversor.DIALOG_ALERTA_ERROR);
		} else {
			double c = Double.parseDouble(s);
			double f = 1.8 * c + 32; 
			campoFar.setText("" + f);					
		}
	}

	protected void farenheitToCelsius() {
		String s = campoFar.getText().toString();
		if (s.equals("")) {
			campoCel.setText("");
			Toast.makeText(getApplicationContext(), 
					R.string.error_valor_nulo, 
					Toast.LENGTH_SHORT).show();
		} else {
			double f = Double.parseDouble(s);
			double c = (f - 32)/1.8;
			campoCel.setText("" + c);					
		}
	}

	protected void salir() {
		setResult(RESULT_OK);
		finish();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
	    Dialog dialog;
	    switch(id) {
	    case DIALOG_ALERTA_ERROR:
	    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    	builder.setMessage(R.string.error_valor_nulo)
	    	       .setCancelable(false)
	    	       .setNeutralButton(R.string.aceptar,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					});
	    	dialog = builder.create();
	        break;
	    default:
	        dialog = null;
	    }
	    return dialog;
	}

}